package com.tanguy.driver.net.http

import android.content.Context
import cn.sinata.xldutil_kotlin.net.RRetrofit
import cn.sinata.xldutils_kotlin.data.ResultData
import com.alibaba.sdk.android.oss.OSSClient
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.google.gson.JsonObject
import com.tanguy.driver.App.Companion.app
import com.tanguy.driver.Const
import com.tanguy.driver.bean.DriverPageBean
import com.tanguy.driver.bean.DriverRegister
import com.tanguy.driver.bean.GrabOrder
import com.tanguy.driver.bean.car.CarBrandBean
import com.tanguy.driver.bean.car.CarModelBean
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import java.io.File


/**
 *
 */
object HttpManager {

    val decodeDes: Boolean = false
    val PAGE_SIZE = 15

    /**
     * OSS上传图片
     */
    fun uploadFile(context: Context, fileUrl: String): Observable<String> {
        val file = File(fileUrl)
        // 明文设置secret的方式建议只在测试时使用，更多鉴权模式请参考后面的`访问控制`章节
        val credentialProvider = OSSPlainTextAKSKCredentialProvider(Const.KEY.OSS_AK, Const.KEY.OSS_AKS)
        val oss = OSSClient(context.applicationContext, Const.KEY.OSS_END_POINT, credentialProvider)
        val objectKey = "tangguy/image/" + System.currentTimeMillis() + "." + file.name.substring(file.name.lastIndexOf(".") + 1)
        // 构造上传请求
        val put = PutObjectRequest(Const.KEY.BUCKET_NAME, objectKey, file.path)
        return Observable.just(oss)
                .observeOn(Schedulers.io())
                .map {
                    it.putObject(put)
                    oss.presignPublicObjectURL(Const.KEY.BUCKET_NAME, objectKey)
                }
    }

    /**
     * 获取短信验证码接口
     * @param type 类型【1：登陆 2:修改密码 3:更换手机 4:司机注册】
     */
    fun sendSmsCode(phone: String, type: Int): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).sendSmsCode(phone, type)
    }

    /**
     * 验证验证码
     * @param type 【1：登陆 2:修改密码 3:更换手机 4:司机注册】
     */
    fun yzCode(phone: String, type: Int, code: String): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).yzCode(phone, type, code)
    }

    /**
     *  获得车辆品牌
     *
     */
    fun getBrandList(): Observable<ResultData<ArrayList<CarBrandBean>>> {
        return RRetrofit.instance().create(ApiService::class.java).getBrandList()
    }

    /**
     * 获得车辆类型
     */
    fun getModelList(id: Int): Observable<ResultData<ArrayList<CarModelBean>>> {
        return RRetrofit.instance().create(ApiService::class.java).getModelList(id.toString())
    }

    /**
     * 司机加盟
     */
    fun driverRegister(driverRegister: DriverRegister): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).driverRegister(
                driverRegister.name
                , driverRegister.sex
                , driverRegister.idcard
                , driverRegister.drivage
                , driverRegister.licenceUrl
                , driverRegister.modelId
                , driverRegister.color
                , driverRegister.plate
                , driverRegister.careful
                , driverRegister.travelUrl
                , driverRegister.insuranceUrl
                , driverRegister.peoplecarUrl
                , driverRegister.phone
                , driverRegister.password
        )
    }

    /**
     * 登陆
     */
    fun login(phone: String, password: String): Observable<ResultData<JsonObject>> {
        return RRetrofit.instance().create(ApiService::class.java).login(phone, password)
    }

    /**
     * 忘记密码
     */
    fun updatePasw(phone: String, password: String): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).updatePasw(phone, password)
    }

    /**
     * 获取司机主页
     */
    fun homepage(id: Int): Observable<ResultData<DriverPageBean>> {
        return RRetrofit.instance().create(ApiService::class.java).homepage(id)
    }

    /**
     * 下班
     */
    fun afterWork(id: Int): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).afterWork(id)
    }

    /**
     * 上班
     */
    fun goWork(id: Int): Observable<ResultData<Any>> {
        return RRetrofit.instance().create(ApiService::class.java).goWork(id)
    }

    /**
     * 司机抢单
     */
    fun grabOrder(orderId: String): Observable<ResultData<GrabOrder>> {
        return RRetrofit.instance().create(ApiService::class.java).grabOrder(orderId, app.userId.toString(), app.latitude, app.longitude)
    }
    /**
     * 司机参加排队
     */
    fun lineup(type: String,linid: String): Observable<ResultData<JsonObject>> {
        return RRetrofit.instance().create(ApiService::class.java).lineup(app.userId.toString(),type,linid, app.code, app.latitude, app.longitude)
    }
    /**
     * 司机参加排队
     */
    fun cenlLineUp(): Observable<ResultData<Any?>> {
        return RRetrofit.instance().create(ApiService::class.java).cenlLineUp(app.userId.toString(),app.lastAreaType.toString(),app.lastAreaID.toString())
    }
}